package com.im.activity;

import org.jivesoftware.smack.packet.Presence;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.core.sdk.core.BaseActivity;
import com.core.sdk.core.BaseEvent;
import com.core.sdk.core.UITask;
import com.core.sdk.utils.ToastUtil;
import com.im.app.ImApplication;
import com.im.event.FriendAddResponseEvent;
import com.im.event.PresenceEvent;
import com.im.service.SmackService;

public abstract class MyBaseActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public void executeEvent(BaseEvent evt) {
		super.executeEvent(evt);
		if(evt instanceof PresenceEvent){
			PresenceEvent pe = (PresenceEvent)evt;
			//有人请求加我为好友
			postRunOnUi(new UITask(this,pe.getPresence()) {
				@Override
				public void run() {
					Presence pp = (Presence)getData();
					executePresenceEventOnUiThread(pp,getContext());
				}
			});
		}
	}
	
	protected void executePresenceEventOnUiThread(Presence pre,Context ctx){
		String from = pre.getFrom();
		
		if(pre.getType().ordinal() == Presence.Type.subscribe.ordinal()){
			showAddFriendDialog(ctx,"提示",from+" 请求添加我为好友！是否同意?");
		}else if(pre.getType().ordinal() == Presence.Type.subscribed.ordinal()){
			ToastUtil.show(ctx, from+" 同意了我的请求！");
		}else if(pre.getType().ordinal() == Presence.Type.unsubscribe.ordinal()){
			ToastUtil.show(ctx, from+" 拒绝了我的请求");
		}else if(pre.getType().ordinal() == Presence.Type.unsubscribed.ordinal()){
			ToastUtil.show(ctx, from+" 从ta的好友列表中删除了我");
		}else if(pre.getType().ordinal() == Presence.Type.unavailable.ordinal()){
//			ToastUtil.show(ctx, from+" 下线了");
		}else if(pre.getType().ordinal() == Presence.Type.available.ordinal()){
//			ToastUtil.show(ctx, from+" 上线了");
		}
	}
	
	protected void showAddFriendDialog(Context context,String title,String content){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setNegativeButton("拒绝", new AlertDialog.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				FriendAddResponseEvent fare = new FriendAddResponseEvent(findLocation(SmackService.class), FriendAddResponseEvent.ARGEE.no);
				sendEvent(fare);
			}
		});
		builder.setPositiveButton("同意", new AlertDialog.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				FriendAddResponseEvent fare = new FriendAddResponseEvent(findLocation(SmackService.class), FriendAddResponseEvent.ARGEE.yes_add);
				sendEvent(fare);
			}
		});
		builder.setTitle(title);
		builder.setMessage(content);
		builder.create();
		builder.show();
		
	}
	
	protected String getUserJID(){
		return ((ImApplication) getApplicationContext()).getUserJID();
	}
}
